<?php


if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){

$db = new Mypdo();
$loyerManager = new LoyerManager($db);

if(!empty($_POST['ligne'])){
    $_SESSION['ligne'] = $_POST['ligne'];
}
if(!empty($_POST['id_pers'])||!empty($_POST['id_loc'])){


var_dump($_POST);
    $ligne = $_SESSION['ligne'];

    if(!empty($_POST['id_pers'])){
        $_POST['id_pers'] = $_POST['id_pers'] == 'Sélectionner...'?'':$_POST['id_pers'];
        $_SESSION['id_pers'.$ligne] = $_POST['id_pers'];
    }
    if(!empty($_POST['id_loc'])){
        $_POST['id_loc'] = $_POST['id_loc'] == 'Sélectionner...'?'':$_POST['id_loc'];
        $loyer = $loyerManager->getLoyerByMoisLocLoca($_SESSION['date'.$ligne],$_POST['id_loc'],$_SESSION['id_loca'.$ligne]);

        $_SESSION['id_loc'.$ligne] = $_POST['id_loc'];
        $_SESSION['date_loyer'.$ligne] = $loyer->getDateLoyer();


    }
    try{
        var_dump($_SESSION);

    $imp = $loyerManager->etatLoyerByLocaLocDate($_SESSION['id_loca'.$ligne],$_SESSION['id_loc'.$ligne],$_SESSION['date'.$ligne]);

        if(!$imp){
            header('Location: include/pages/impAppel.php');
        }else{
            header('Location: include/pages/impQuit.php');
        }
    }catch (ImmolocException $e){
        header('Location: #');
    }

/*
var_dump( $_SESSION['id_pers']);
   var_dump( $_SESSION['id_loc']);
var_dump($_SESSION['date_loyer']);*/

}else{
?>
<h1>Appels de loyers</h1>
<form id="table" action="#" method="post">
    <label for="date">Sélectionner un mois : </label>
    <select name="date" id="date" onchange="document.getElementById('table').submit();">
        <?php if(!$_POST){ ?><option></option><?php } ?>
        <?php foreach ($loyerManager->recupMois() as $attribue => $value) {
            ?><option <?php if($_POST){ echo $_POST['date'] == $attribue?'selected':''; } ?> value="<?php echo $attribue; ?>"><?php echo $value; ?></option><?php echo "\n";
        }
        ?>

    </select>
    <input id="appel" name="appel" type="button" onclick="document.location.href='index.php?page=210'" value="Nouvel Appel" />

</form>
<?php
if($_POST){



    $locaManager = new LocataireManager($db);
    $loca = $locaManager->getListByMoisLoyer($_POST['date']);

    ?>
    <h2>Appels Effectuer</h2>
    <table class="tableau" >
        <tr>
            <th>Nom</th>
            <th>Prenom</th>
            <th>Location</th>
            <th>Etat</th>
            <th>Imprimer</th>
            <th>Détail</th>
        </tr>
        <?php
        $ligne = 0;
        foreach ($loca as $valeur) {
            $ligne++;
            $_SESSION['ligne_max'] = $ligne;
            $loc2 = $locaManager->getLocationAppeler($valeur->getIdLoca(), $_POST['date']);

            $loc = $locaManager->getLocation($valeur->getIdLoca());
            $pers = $locaManager->getPersonne($valeur->getIdLoca());

//var_dump($pers);
            ?>

            <tr>
                <td><?php
                    if(gettype($pers) == 'object'){
                        echo $pers->getNomPers();
                    }else{
                        foreach($pers as $val){
                            echo $val->getNomPers().'<br/>';
                        }
                    }

                    ?></td>
                <td><?php
                    if(gettype($pers) == 'object'){
                        echo $pers->getPrenomPers();
                    }else{
                        foreach($pers as $val){
                            echo $val->getPrenomPers().'<br/>';
                        }
                    }?></td>
                <td><?php
                    if(gettype($loc) == 'object'){
                        echo $loc->getNomLoc();
                    }else{
                        foreach($loc as $val){
                            echo $val->getNomLoc().'<br/>';
                        }
                    }
                    ?></td>
                <td><?php
                    $imp = false;
                    $appel = false;

                    $duo = false;

                    if(gettype($loc) == 'object'){
                        try{
                            $imp = $loyerManager->etatLoyerByLocaLocDate($valeur->getIdLoca(),$loc->getIdLoc(),$_POST['date']) == 0;
                            echo $imp?'<img id="ico" src="image/erreur.png" alt="Non Payer">':'<img id="ico" src="image/valid.png" alt="Payer">';
                            $appel = true;
                        }catch(ImmolocException $e){
                            echo '<img id="ico" src="image/interdit.png" alt="'.$e->getMessage().'">';
                        }
                    }else{
                        $tp = '';
                        foreach($loc as $val){
                            try{
                                $imp = $loyerManager->etatLoyerByLocaLocDate($valeur->getIdLoca(),$val->getIdLoc(),$_POST['date']) == 0;
                                echo $imp?'<img id="ico" src="image/erreur.png" alt="Non Payer">':'<img id="ico" src="image/valid.png" alt="Payer">';
                                $appel = true;
                                if($tp === ''){
                                    $tp = $imp;
                                }elseif($tp != $imp){
                                    $duo = true;
                                }
                            }catch(ImmolocException $e){
                                echo '<img id="ico" src="image/interdit.png" alt="'.$e->getMessage().'">';
                                //echo '<img id="ico" src="image/ajouter.png" alt="'.$e->getMessage().'">';

                            }
                        }

                    }

                    ?>

                </td>
                <td>
                    <?php if($imp||!$imp&&$appel){
                        //var_dump($loc2);
                        $type = $imp?'Appel':'Quit';
                        $one = true;
                        $_SESSION['id_loca'.$ligne] = $valeur->getIdLoca();
                        $_SESSION['date'.$ligne] = $_POST['date'];
                        ?><form method="post" action="#" class="form_imp" target="_blank" ><?php
                        if(gettype($pers) == 'object'){
                            $_SESSION['id_pers'.$ligne] = $pers->getIdPers();
                        }else{
                            $one = false;
                            ?>
                            <label for="id_pers" hidden="" class="changer_imp<?php echo $ligne; ?>">Personne : </label>
                            <select id="id_pers" name="id_pers" required="required" hidden="" class="changer_imp<?php echo $ligne; ?>">
                            <option>Sélectionner...</option><?php
                            foreach ($pers as $value) {
                                ?><option value="<?php echo $value->getIdPers(); ?>"><?php echo $value->getNomPers().' '.$value->getPrenomPers(); ?></option><?php
                            }
                        ?></select><?php
                        }
                        if(gettype($loc2) == 'object'){
                            $loyer = $loyerManager->getLoyerByMoisLocLoca($_POST['date'],$loc2->getIdLoc(),$valeur->getIdLoca());
                            $_SESSION['id_loc'.$ligne] = $loc2->getIdLoc();
                            $_SESSION['date_loyer'.$ligne] = $loyer->getDateLoyer();


                        }else{
                            $one = false;
                            ?>
                            <label for="id_loc" hidden="" class="changer_imp<?php echo $ligne; ?>">Location : </label>
                            <select id="id_loc" name="id_loc" required="required" hidden="" class="changer_imp<?php echo $ligne; ?>">
                            <option>Sélectionner...</option><?php
                            foreach ($loc2 as $value) {
                                ?><option value="<?php echo $value->getIdLoc(); ?>"><?php echo $value->getNomLoc(); ?></option><?php
                            }
                            ?></select><?php
                        }
                        ?><input name="imp" id="imp-<?php echo $ligne; ?>" class="change_imp change_imp<?php echo $ligne; ?>" type="button" value="<?php if($duo){echo 'Appel/Quittance';}else{echo $type == 'Appel'?'Appel':'Quittance';} ?>" <?php echo $one?'onclick="window.open(\'include/pages/imp'.$type.'.php?ligne='.$ligne.'\')"':'' ?> /><?php
                        ?><input type="submit" class="submit<?php echo $ligne; ?>" style="display:none;">
                        </form><?php
                    }

                    ?>
                </td>

                <td><input id="detail" name="detail" type="button" onclick="document.location.href='index.php?page=114&id=<?php echo $valeur->getIdLoca(); ?>'" />
                <?php if($imp&&$appel){
                    ?><td><?php
                        if(gettype($loc) == 'object'){?>
                                <input name="aqui" id="aqui" type="button" value="Acquitter" onclick="document.location.href='index.php?page=211&idla=<?php echo $valeur->getIdLoca(); ?>&idl=<?php echo $loc->getIdLoc(); ?>&date=<?php echo $_POST['date']; ?>'"/>
                <?php   }else{
                            foreach ($loc as $value) {
                                try{
                                    if($loyerManager->etatLoyerByLocaLocDate($valeur->getIdLoca(),$value->getIdLoc(),$_POST['date'])==0){
                                    ?>
                                    <input name="aqui" id="aqui" type="button" value="Acquitter" onclick="document.location.href='index.php?page=211&idla=<?php echo $valeur->getIdLoca(); ?>&idl=<?php echo $value->getIdLoc(); ?>&date=<?php echo $_POST['date']; ?>'"/>

                                <?php }
                                }catch(ImmolocException $e){}

                            }


                        }
                    ?>
                    </td>
                <?php

                }?>
            </tr>

        <?php
        } ?>
    </table>



     <?php

?>
    <?php
    $locaManager = new LocataireManager($db);
    $loca = $locaManager->getListByAppelLoyer($_POST['date']);
    if(!empty($loca)){
    ?>
<h2>Appels en Attentes</h2>


<table class="tableau" >
    <tr>
        <th>Nom</th>
        <th>Prenom</th>
        <th>Location</th>
        <th>Détail</th>
    </tr>
    <?php foreach ($loca as $valeur) {
        $loc = $locaManager->getLocation($valeur->getIdLoca());
        $pers = $locaManager->getPersonne($valeur->getIdLoca());

//var_dump($pers);
        ?>

        <tr>
            <td><?php
                if(gettype($pers) == 'object'){
                    echo $pers->getNomPers();
                }else{
                    foreach($pers as $val){
                        echo $val->getNomPers().'<br/>';
                    }
                }

                ?></td>
            <td><?php
                if(gettype($pers) == 'object'){
                    echo $pers->getPrenomPers();
                }else{
                    foreach($pers as $val){
                        echo $val->getPrenomPers().'<br/>';
                    }
                }?></td>
            <td><?php
                if(gettype($loc) == 'object'){
                    echo $loc->getNomLoc();
                }else{
                    foreach($loc as $val){
                        echo $val->getNomLoc().'<br/>';
                    }
                }
                ?></td>

            <td><input id="detail" name="detail" type="button" onclick="document.location.href='index.php?page=114&id=<?php echo $valeur->getIdLoca(); ?>'" /></td>
            <td><input id="appel" name="appel" type="button" value="Ajouter Appel" onclick="document.location.href='index.php?page=210&id=<?php echo $valeur->getIdLoca(); ?>'" /></td>
        </tr>

    <?php
    } ?>
</table>
<?php
    }else{
        ?>
    <h2>Tout les appels ont été lancé</h2>
    <?php
    }
}
}
}else{
    header('Location: index.php');
}
